<template>
  <div class="listBox">
    <!-- 轮播图 -->
    <home-banner :bannerList="bannerList"></home-banner>
    <div class="home-category">
      <ul class="menu">
        <li
          @mouseover="categoryId(item.id)"
          v-for="item in menuList"
          :key="item.id"
        >
          <RouterLink :to="`/category/${item.id}`">{{ item.name }}</RouterLink>
          <template v-if="item.children">
            <RouterLink
              v-for="sub in item.children"
              :key="sub.id"
              :to="`/category/sub/${sub.id}`"
            >
              {{ sub.name }}
            </RouterLink>
          </template>
          <!-- 骨架 -->
          <template v-else>
            <XtxSkeleton
              width="60px"
              height="18px"
              style="margin-right:5px"
              bg="rgba(255,255,255,0.2)"
            />
            <XtxSkeleton
              width="50px"
              height="18px"
              bg="rgba(255,255,255,0.2)"
            />
          </template>
        </li>
      </ul>
    </div>
    <!-- 弹层 -->
    <div class="layer">
      <h4>分类推荐 <small>根据您的购买或浏览记录推荐</small></h4>
      <ul>
        <li v-for="i in twoClassIfy.goods" :key="i">
          <RouterLink to="/">
            <img :src="i.picture" alt="" />
            <div class="info">
              <p class="name ellipsis-2">{{ i.name }}</p>
              <p class="desc ellipsis">{{ i.desc }}</p>
              <p class="price"><i>¥</i>{{ i.price }}</p>
            </div>
          </RouterLink>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { getClassify, getHeader, homeBannerObj } from "@/api";
import { ref, reactive } from "vue";
import homeBanner from "../../../components/home/homeBanner.vue";
export default {
  name: "HomeCategory",
  setup() {
    let menuList = ref([]); // 分类数据
    let twoClassIfy = ref([]); // 2级分类商品
    let obj = ref([]); // 备份一份数据

    //   请求轮播图
    let bannerList = ref([]);
    homeBannerObj().then((res) => {
      bannerList.value = res.result;
    });

    const brand = reactive({
      id: "brand",
      name: "品牌",
      children: [{ id: "brand-chilren", name: "品牌推荐" }],
    });
    // 少一商品往后追加一个
    getClassify().then((res) => {
      menuList.value = res.result.map((item) => {
        return {
          id: item.id,
          name: item.name,
          // 防止初始化没有children的时候调用slice函数报错
          children: item.children && item.children.slice(0, 2),
          goods: item.goods,
        };
      });
      menuList.value.push(brand);
    });
    getHeader().then((res) => {
      obj.value = res.result;
    });
    // hover 的函数   每次悬停后的id
    function categoryId(ids) {
      //   console.log(menuList.value);
      twoClassIfy.value = obj.value.find((item) => {
        if (item.id == ids) {
          return item.goods;
        }
      });
    }
    // 分类弹框数据

    return {
      menuList,
      categoryId,
      twoClassIfy,
      obj,
      bannerList,
    };
  },
  components: {
    homeBanner,
  },
};
</script>

<style scoped lang="scss">
.listBox {
  .home-category {
    width: 250px;
    height: 500px;
    background: rgba(0, 0, 0, 0.8);
    position: relative;
    z-index: 99;

    .menu {
      li {
        padding-left: 40px;
        height: 50px;
        line-height: 50px;

        &:hover {
          background: $xtxColor;
        }
        a {
          margin-right: 4px;
          color: #fff;
          &:first-child {
            font-size: 16px;
          }
        }
      }
    }
  }
  // 弹框
  .layer {
    width: 990px;
    height: 500px;
    background: rgba(255, 255, 255, 0.8);
    position: absolute;
    left: 250px;
    top: 0;
    display: none;
    padding: 0 15px;
    z-index: 99999;
    h4 {
      font-size: 20px;
      font-weight: normal;
      line-height: 80px;
      small {
        font-size: 16px;
        color: #666;
      }
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 310px;
        height: 120px;
        margin-right: 15px;
        margin-bottom: 15px;
        border: 1px solid #eee;
        border-radius: 4px;
        background: #fff;
        &:nth-child(3n) {
          margin-right: 0;
        }
        a {
          display: flex;
          width: 100%;
          height: 100%;
          align-items: center;
          padding: 10px;
          &:hover {
            background: #e3f9f4;
          }
          img {
            width: 95px;
            height: 95px;
          }
          .info {
            padding-left: 10px;
            line-height: 24px;
            width: 190px;
            .name {
              font-size: 16px;
              color: #666;
            }
            .desc {
              color: #999;
            }
            .price {
              font-size: 22px;
              color: $priceColor;
              i {
                font-size: 16px;
              }
            }
          }
        }
      }
    }
  }
  //   hover控制元素的显示和隐藏不生效
  // 原因：点击的元素和需要显示隐藏的元素不属于一个父级元素，
  //   如果想要hover伪类生效的话，点击的元素和显示隐藏的元素需要属于同一个父级元素，例如。

  //   &:hover {
  //     .layer {
  //       display: block;
  //     }
  //   }

  .home-category:hover + .layer {
    //  兄弟之间隐藏
    display: block;
  }
}
</style>
